/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ivncorp.evnproject.chart;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

import com.ivncorp.evnproject.R;
import com.ivncorp.evnproject.helpers.Variables;
import com.ivncorp.evnproject.model.ObjectCot;

/**
 * Sales demo bar chart.
 */
public class SalesStackedBarChartCot extends AbstractChart {
  /**
   * Returns the chart name.
   * 
   * @return the chart name
   */
  public String getName() {
    return "Biá»ƒu Ä‘á»“ thá»‘ng kÃª Cá»™t";
  }

  public String getDesc() {
    return "Biá»ƒu Ä‘á»“ nÄƒm 2014";
  }

  public Intent execute(Context context) {
    String[] titles = new String[] { context.getString(R.string.t_ng_s_thu_th_p), context.getString(R.string.t_ng_s_t_i_l_n) };
    List<double[]> values = new ArrayList<double[]>();
    
    double[] listSum = new double[] {0,0,0,0,0,0,0,0,0,0,0,0,0};
    double[] listSumUploaded = new double[] {0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    double max = 0.0;
    int uploaded = 0;
    for (ObjectCot cot : Variables.currentListObjectCot){
    	int index = 0;
    	index = Integer.parseInt(cot.getTimestamp().substring(5, 7)) - 1;
    	listSum[index]++;
    	if (cot.getUploaded() != 0){
    		listSumUploaded[index]++;
    		uploaded++;
    	}
    	if (max < listSum[index]) max = listSum[index];
    }
    
    values.add(listSum);
    values.add(listSumUploaded);
    
    int[] colors = new int[] { Color.BLUE, Color.CYAN };
    XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
    setChartSettings(renderer, context.getString(R.string.thong_ke_thu_thap_cot_nam_2015_) + uploaded + "/" + Variables.currentListObjectCot.size() + ")", context.getString(R.string.thang), context.getString(R.string.so_luong), 0.5,
        12.5, 0, max + 10, Color.GRAY, Color.LTGRAY);
    renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
    renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
    renderer.setXLabels(12);
    renderer.setYLabels(10);
    renderer.setXLabelsAlign(Align.LEFT);
    renderer.setYLabelsAlign(Align.LEFT);
    renderer.setPanEnabled(true, false);
    renderer.setZoomEnabled(false);
    renderer.setZoomRate(1.1f);
    renderer.setBarSpacing(0.5f);
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.BLACK);
    
    return ChartFactory.getBarChartIntent(context, buildBarDataset(titles, values), renderer,
        Type.STACKED);
  }

}
